import * as loginService from '../../services/login';

export default {

  namespace: 'login',

  state: {
    isLogin : false,
  },

  effects: {
    *login({ payload }, { call, put }) {
      const rsp = yield call(loginService.login, payload);
      console.log('loginService login');
      console.log(rsp);
      yield put({ type: 'loginR', payload: { loginData: rsp.data } });


    },
    *logout({ _ }, { call, put }) {
      console.log('loginService logout');
      yield call(loginService.logout());
      yield put({ type: 'logoutR' });
      return ;
    },
    // *addOne({ payload }, { call, put }) {
    //   const rsp = yield call(groupService.addOne, payload);
    //   yield put({ type: 'queryList' });
    //   return rsp;
    // },
    // *updateOne({ payload }, { call, put }) {
    //   console.log(`groupService update` + payload);
    //   const rsp = yield call(groupService.updateOne, payload);
    //   yield put({ type: 'queryList' });
    //   return rsp;
    // }
  },

  reducers: {
    loginR( state, { payload: { loginData } } ) {
      console.log("login success ? " + (loginData.code === 0));
      let isLogin = false;
      if(loginData.code === 0) {
        isLogin = true;
        return {
          ...state,
          isLogin,
        }
      } else {
        return {
          ...state,
          isLogin,
        }
      }
    }
  },

  // subscriptions: {
  //   setup({ dispatch}){
  //     dispatch({
  //       type: 'queryList',
  //     });
  //   }
  // },
};
